package com.zlsx.androidutil.androidview.buttonview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatButton;
import com.zlsx.androidutil.R;
/**
 * @author zlsx
 * @time: 2019/8/26 11:04
 * @Description:
 **/
public class ProgressButton extends AppCompatButton {
    private Paint mPaint = new Paint();

    private int mProgress;

    public ProgressButton(Context context) {
        super(context);
    }

    public ProgressButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(getContext().getResources().getColor(R.color.button_yellow_green));
        mPaint.setAntiAlias(true);
        mPaint.setAlpha(128);
        mPaint.setStrokeWidth(1.0f);
        Rect rect = new Rect();
        canvas.getClipBounds(rect);
        rect.left += getPaddingLeft();
        rect.top += getPaddingTop();
        rect.right = (rect.left - getPaddingLeft()) + (mProgress * getWidth() / 100) - getPaddingRight();
        rect.bottom -= getPaddingBottom();
        canvas.drawRoundRect(new RectF(rect), 8.0f, 8.0f, mPaint);
        super.onDraw(canvas);
    }

    public void updateProgress(int progress) {
        if (progress >= 0 && progress <= 100) {
            mProgress = progress;
            invalidate();
        } else if (progress < 0) {
            mProgress = 0;
            invalidate();
        } else if (progress > 100) {
            mProgress = 100;
            invalidate();
        }
    }
    public int getmProgress() {
        return mProgress;
    }
}